package com.ruoyi.admin.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.admin.domain.ZhxComment;
import com.ruoyi.admin.domain.ZhxLikeTopic;
import com.ruoyi.admin.mapper.ZhxLikeTopicMapper;
import com.ruoyi.common.core.domain.entity.User;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.admin.mapper.ZhxTopicMapper;
import com.ruoyi.admin.domain.ZhxTopic;
import com.ruoyi.admin.service.IZhxTopicService;

/**
 * 话题管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-11
 */
@Service
public class ZhxTopicServiceImpl implements IZhxTopicService 
{
    @Autowired
    private ZhxTopicMapper zhxTopicMapper;

    @Autowired
    private ZhxLikeTopicMapper zhxLikeTopicMapper;

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询话题管理
     * 
     * @param topicId 话题管理主键
     * @return 话题管理
     */
    @Override
    public ZhxTopic selectZhxTopicByTopicId(Long topicId)
    {
        return zhxTopicMapper.selectZhxTopicByTopicId(topicId);
    }

    /**
     * 查询话题管理列表
     * 
     * @param zhxTopic 话题管理
     * @return 话题管理
     */
    @Override
    public List<ZhxTopic> selectZhxTopicList(ZhxTopic zhxTopic)
    {
        return zhxTopicMapper.selectZhxTopicList(zhxTopic);
    }

    /**
     * 查询话题列表
     * 根据点赞量(likeNum)从高到低排序
     * @param zhxTopic 话题
     * @return 话题
     */
    @Override
    public List<ZhxTopic> selectZhxTopicListByLike(ZhxTopic zhxTopic)
    {
        List<ZhxTopic> zhxTopics = zhxTopicMapper.selectZhxTopicListByLike(zhxTopic);

        Long sysUserId = 0L;
        if (SecurityUtils.getUnUserId() != -1) {
            sysUserId = SecurityUtils.getUserId();
        }

        // 获取脑洞点赞信息
        Map<String, Long> zhxLikeTopicCache = new HashMap<>();
        if (redisCache.getCacheObject("zhxLikeTopicCache") != null) {
            zhxLikeTopicCache = redisCache.getCacheObject("zhxLikeTopicCache");
        } else {
            List<ZhxLikeTopic> zhxLikeTopics = zhxLikeTopicMapper.selectZhxLikeTopicList(new ZhxLikeTopic());

            for (ZhxLikeTopic zhxLikeTopic : zhxLikeTopics) {
                zhxLikeTopicCache.put(zhxLikeTopic.getUserId() + "-" + zhxLikeTopic.getTopicId(),1L);
            }
            redisCache.setCacheObject("zhxLikeTopicCache", zhxLikeTopicCache);
        }

        for (ZhxTopic topic : zhxTopics) {
            // 从缓存中获取用户信息，如果没有则从数据库中查询并存入缓存
            User user = redisCache.getCacheObject("user:" + topic.getUserId());;
            if (user == null) {
                user = userMapper.selectUserByIdEasy(topic.getUserId());
                redisCache.setCacheObject("user:" + user.getUserId(), user);
            }
            topic.setSysUser(user);
            topic.setIsLike(zhxLikeTopicCache.get(sysUserId + "-" + topic.getTopicId()) == null ? 0 : 1);
        }
        return zhxTopics;
    }

    /**
     * 新增话题管理
     * 
     * @param zhxTopic 话题管理
     * @return 结果
     */
    @Override
    public int insertZhxTopic(ZhxTopic zhxTopic)
    {
        zhxTopic.setCreateTime(DateUtils.getNowDate());
        return zhxTopicMapper.insertZhxTopic(zhxTopic);
    }

    /**
     * 新增话题管理
     *
     * @param zhxTopic 话题管理
     * @return 结果
     */
    @Override
    public int insertZhxTopicTodo(ZhxTopic zhxTopic)
    {
        zhxTopic.setUserId(SecurityUtils.getUserId());
        zhxTopic.setCreateTime(DateUtils.getNowDate());
        return zhxTopicMapper.insertZhxTopic(zhxTopic);
    }

    /**
     * 修改话题管理
     * 
     * @param zhxTopic 话题管理
     * @return 结果
     */
    @Override
    public int updateZhxTopic(ZhxTopic zhxTopic)
    {
        zhxTopic.setUpdateTime(DateUtils.getNowDate());
        return zhxTopicMapper.updateZhxTopic(zhxTopic);
    }

    /**
     * 批量删除话题管理
     * 
     * @param topicIds 需要删除的话题管理主键
     * @return 结果
     */
    @Override
    public int deleteZhxTopicByTopicIds(Long[] topicIds)
    {
        return zhxTopicMapper.deleteZhxTopicByTopicIds(topicIds);
    }

    /**
     * 删除话题管理信息
     * 
     * @param topicId 话题管理主键
     * @return 结果
     */
    @Override
    public int deleteZhxTopicByTopicId(Long topicId)
    {
        return zhxTopicMapper.deleteZhxTopicByTopicId(topicId);
    }

    /**
     * 获取话题数量
     */
    @Override
    public int getTopicNum() {
        return zhxTopicMapper.getTopicNum();
    }

    /**
     * 获取今日新增话题数量
     */
    @Override
    public int getTopicNumToday() {
        return zhxTopicMapper.getTopicNumToday();
    }


    

}
